//
//  PLNavigationController.m
//  HD_OA
//
//  Created by hd_pl on 16/9/5.
//  Copyright © 2016年 hd_pl. All rights reserved.
//

#import "PLNavigationController.h"
//#import "UIImage+Image.h"
#define HDMainColor [UIColor colorWithRed:49 / 255.0 green:161 / 255.0 blue:176 / 255.0 alpha:1.0]



@interface PLNavigationController ()

@end
@implementation PLNavigationController

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithRootViewController:rootViewController]) {
    }

    return self;
}

+ (void)initialize {
    UIBarButtonItem *itemAppearance = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[
                                           [self class]
    ]];
    NSMutableDictionary *normal_dic = [NSMutableDictionary dictionary];

    normal_dic[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    normal_dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [itemAppearance setTitleTextAttributes:normal_dic forState:UIControlStateNormal];

    //设置disable颜色
    NSDictionary *disableDic = @{
            NSForegroundColorAttributeName: HDMainColor
    };
    [itemAppearance setTitleTextAttributes:disableDic forState:UIControlStateDisabled];



    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[
                                [self class]
    ]];
    [bar setShadowImage:[UIImage new]];//取消分割线
    [bar setBackgroundImage:[UIImage imageWithColor:HDMainColor] forBarMetrics:UIBarMetricsDefault];
    bar.barStyle = UIBarStyleDefault;
    [bar setTintColor:[UIColor whiteColor]];
    NSMutableDictionary *dicBar = [NSMutableDictionary dictionary];
    dicBar[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    dicBar[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [bar setTitleTextAttributes:dicBar];
    bar.translucent = NO;

    if (@available(iOS 13, *)) {
        UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc]init];
        [navBarAppearance setBackgroundImage:[UIImage imageWithColor:HDMainColor]];
        navBarAppearance.titleTextAttributes = @{
                NSFontAttributeName: [UIFont systemFontOfSize:15],
                NSForegroundColorAttributeName: [UIColor whiteColor]
        };
        navBarAppearance.shadowColor = UIColor.clearColor;
        //去掉半透明
        navBarAppearance.backgroundEffect = nil;
        //去除阴影线
        navBarAppearance.shadowColor = [UIColor clearColor];
        bar.scrollEdgeAppearance = navBarAppearance;
        bar.standardAppearance = navBarAppearance;
        bar.compactAppearance = navBarAppearance;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSUInteger count = self.viewControllers.count;

    if (count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [self createBackButton];
    }

    return [super pushViewController:viewController animated:animated];
}

#pragma mark 返回按钮
- (void)popself {
    @try {
        [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    } @catch (NSException *exception) {
        NSLog(@"%@",exception);
    } @finally {
        
    }
    
}

#pragma mark 创建返回按钮
- (UIBarButtonItem *)createBackButton {
    return [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_previous"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(popself)];
}

@end



@implementation UIImage (image)

+ (UIImage *)imageWithColor:(UIColor *)color {
    //描述一个矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

    //获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //使用color演示填充上下文
    CGContextSetFillColorWithColor(ctx, [color CGColor]);

    //渲染上下文
    CGContextFillRect(ctx, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    //关闭图形上下文
    UIGraphicsEndImageContext();

    return image;
}

@end
